﻿using System;

using System.Collections.Generic;
using System.Text;

namespace Handrise.Mobile.BasicModal.GPS
{

    /// <summary>
    /// 计算坐标点间直线距离的类
    /// </summary>
    internal class GetDistance
    {
        /// <summary>
        /// 计算两个坐标点的距离
        /// </summary>
        /// <param name="lng1">第一个经度</param>
        /// <param name="lat1">第一个纬度</param>
        /// <param name="lng2">第一个经度</param>
        /// <param name="lat2">第一个纬度</param>
        /// <param name="gs">高斯投影中所选用的参考椭球</param>
        /// <returns>返回距离,返回-1时经纬度格式错误</returns>
        internal static double DistanceOfTwoPoints(double lng1, double lat1, double lng2, double lat2, GaussSphere gs)
        {
            if (lng1 > 180 || lng1 < 0 || lat1 > 90 || lat1 < 0 || lng2 > 180 || lng2 < 0 || lat2 > 90 || lat2 < 0)
            {
                return -1; ;
            }
            double radLat1 = Rad(lat1);
            double radLat2 = Rad(lat2);
            double a = radLat1 - radLat2;
            double b = Rad(lng1) - Rad(lng2);
            double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) +
             Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2)));
            s = s * (gs == GaussSphere.WGS84 ? 6378137.0 : (gs == GaussSphere.Xian80 ? 6378140.0 : 6378245.0));
            s = Math.Round(s * 10000) / 10000;
            return s;
        }


        private static double Rad(double d)
        {
            return d * Math.PI / 180.0;
        }
    }
}
